package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfWhiteInfo;
import com.ocom.common.entity.srvpaccess.XfWhiteInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfWhiteInfo;
import com.ocom.payconfig.service.XfWhiteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class XfWhiteInfoServiceImpl extends ServiceImpl<IDaoXfWhiteInfo, EntityXfWhiteInfo> implements XfWhiteInfoService {


    @Autowired
    private IDaoXfWhiteInfo xfWhiteInfo;
    //新增白名单：用comId和pId去查询白名单表xf_white_info是否为空
    //a：不为空，删除消费白名单表xf_white_info所有com_id、p_id匹配的记录。
    //b：为空执行下一步
    //   用com_id=@comId，p_id=@pId，state=0，create_time=系统当前时间，新增入库
    //行级锁
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void insertWhiteInfo(Long comId, Long pId, Long state) {
        QueryWrapper<EntityXfWhiteInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("p_id",pId);
        EntityXfWhiteInfo entityXfWhiteInfo = xfWhiteInfo.selectOne(query);
        //a：不为空，删除消费白名单表xf_white_info所有com_id、p_id匹配的记录。
        if(entityXfWhiteInfo!=null){
            xfWhiteInfo.delete(query);
        }
        //b：为空执行下一步
        //   用com_id=@comId，p_id=@pId，state=0，create_time=系统当前时间，新增入库
        EntityXfWhiteInfo entityXfWhiteInfo1 = new EntityXfWhiteInfo();
        entityXfWhiteInfo1.setComId(comId);
        entityXfWhiteInfo1.setPId(Long.valueOf(pId));
        entityXfWhiteInfo1.setState(state);
        xfWhiteInfo.insert(entityXfWhiteInfo1);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public Integer getXFWhiteVer(Long comId) {
        QueryWrapper<EntityXfWhiteInfo> queryWrapper = Wrappers.query();
        queryWrapper.select("MAX(id) as personVer");
        queryWrapper.eq("com_id", comId);
        EntityXfWhiteInfo entityXfWhiteInfo = this.getOne(queryWrapper);
        return entityXfWhiteInfo.getPersonVer();
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean deleteXfWhite(Long companyId, Integer pId) {
        QueryWrapper<EntityXfWhiteInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        return this.remove(queryWrapper);
    }
}
